using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ASA
{
    public partial class ResultsForm : Form
    {
        private List<AandEDLL.DataAccess.Item> m_searchResults;
        private List<AandEDLL.DataAccess.Item> m_SavedItems;
        private AandEDLL.DataAccess.User m_user;

        public ResultsForm()
        {
            new LoginDialog().ShowDialog();
            InitializeComponent();
            
            /*m_user = AandEDLL.Accessor.AuthenticateUser("Admin", "Password");
            if (m_user == null)
            {
                MessageBox.Show("Could not authenticate user 'Admin'");
            }*/
            UpdateSavedItems();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void adminToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AdminForm adminForm = new AdminForm();
            adminForm.ShowDialog();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            if (tbSearch.Text != "")
            {
                ListViewItem item;

                lvSearchResults.Items.Clear();
                toolStripStatusLabel.Text = "Searching...";

                try
                {
                    m_searchResults = AandEDLL.ItemSearch.searchForItems(tbSearch.Text);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Search for Items failed.\n" + ex.Message);
                }

                if (m_searchResults != null)
                {
                    if (m_searchResults.Count > 0)
                    {
                        foreach (AandEDLL.DataAccess.Item result in m_searchResults)
                        {
                            item = new ListViewItem(result.ItemName);
                            item.SubItems.Add(result.Description);
                            item.SubItems.Add(result.Price.ToString());
                            item.SubItems.Add(result.Currency);
                            item.SubItems.Add(result.URL);
                            item.SubItems.Add(result.SourceName);
                            item.Tag = result;
                            lvSearchResults.Items.Add(item);
                        }
                        toolStripStatusLabel.Text = "Found " + m_searchResults.Count + " results.";
                    }
                    else
                    {
                        toolStripStatusLabel.Text = "";
                    }
                }
                else
                {
                    toolStripStatusLabel.Text = "Search failed.";
                }
            }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            lvSearchResults.Items.Clear();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            ListView.CheckedListViewItemCollection clv = lvSearchResults.CheckedItems;
            if (clv.Count > 0)
            {
                List<AandEDLL.DataAccess.Item> items = new List<AandEDLL.DataAccess.Item>();
                foreach (ListViewItem item in clv)
                {
                    if (m_searchResults.Contains(((AandEDLL.DataAccess.Item)item.Tag)))
                    {
                        try
                        {
                            AandEDLL.DataAccess.Item tmpItem = m_searchResults[m_searchResults.IndexOf((AandEDLL.DataAccess.Item)(item.Tag))];
                            //Cut down the strings to lengths of the database fields.
                            if (tmpItem.ItemName != null)
                            {
                                if (tmpItem.ItemName.Length > 50)
                                    tmpItem.ItemName = tmpItem.ItemName.Substring(0, 50);
                            }
                            if(tmpItem.Description != null)
                            {
                                if (tmpItem.Description.Length > 30)
                                    tmpItem.Description = tmpItem.Description.Substring(0, 30);
                            }
                            if (tmpItem.URL != null)
                            {
                                if (tmpItem.URL.Length > 120)
                                    tmpItem.URL = tmpItem.URL.Substring(0, 120);
                            }

                            
                            if (m_user != null)
                            {
                                tmpItem.User = m_user;
                                items.Add(tmpItem);
                            }
                            else
                                Console.WriteLine("Save Item: user not set. 'Admin'");
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("AandEDLL.Accessor.AuthenticateUser failed.\n" + ex.Message);
                        }
                    }
                }

                if (items.Count > 0)
                {
                    try
                    {
                        AandEDLL.Accessor.SaveItems(items);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Could not Save Item.\n" + ex.Message);
                    }
                }
                UpdateSavedItems();
            }
        }

        public void PrintItem(AandEDLL.DataAccess.Item item)
        {
            Console.Write("\nItem:\n");
            Console.Write("\n\tItemID: {0}", item.ItemID);
            Console.Write("\n\tItemName: {0}", item.ItemName);
            Console.Write("\n\tDescription: {0}", item.Description);
            Console.Write("\n\tCurrency: {0}", item.Currency);
            Console.Write("\n\tPrice: {0}", item.Price);
            Console.Write("\n\tURL: {0}", item.URL);
            Console.Write("\n\tSourceName: {0}", item.SourceName);
            Console.Write("\n\tUserName: {0}", item.User.UserName);
            Console.Write("\n\tUserID: {0}", item.User.UserID);
            Console.Write("\n\tSearchDate: {0}\n\n", item.SearchDate);

        }

        private void btnDeleteItem_Click(object sender, EventArgs e)
        {
            if (m_SavedItems != null)
            {
                ListView.CheckedListViewItemCollection clv = lvItems.CheckedItems;
                if (clv.Count > 0)
                {
                    List<AandEDLL.DataAccess.Item> items = new List<AandEDLL.DataAccess.Item>();
                    foreach (ListViewItem item in clv)
                    {
                        if (m_SavedItems.Contains(((AandEDLL.DataAccess.Item)item.Tag)))
                        {
                            try
                            {
                                AandEDLL.DataAccess.Item tmpItem = m_SavedItems[m_SavedItems.IndexOf((AandEDLL.DataAccess.Item)(item.Tag))];
                                tmpItem.User = m_user;
                                items.Add(tmpItem);
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine("AandEDLL.Accessor.AuthenticateUser failed.\n" + ex.Message);
                            }
                        }

                        if (items.Count > 0)
                        {
                            try
                            {
                                if (!AandEDLL.Accessor.DeleteItems(items))
                                    Console.WriteLine("Could not delete Item(s)");
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine("Could not Save Item.\n" + ex.Message);
                            }
                        }
                    }
                    UpdateSavedItems();
                }
            }
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            UpdateSavedItems();
        }

        private void UpdateSavedItems()
        {
            if (m_SavedItems != null)
                m_SavedItems.Clear();

            lvItems.Items.Clear();

            //AandEDLL.DataAccess.User user = AandEDLL.Accessor.AuthenticateUser("Admin", "Password");
            if (m_user != null)
            {
                m_SavedItems = AandEDLL.Accessor.GetSavedItems(m_user);
                if (m_SavedItems.Count > 0)
                {
                    foreach (AandEDLL.DataAccess.Item result in m_SavedItems)
                    {
                        ListViewItem item = new ListViewItem(result.ItemName);
                        item.SubItems.Add(result.Description);
                        item.SubItems.Add(result.Price.ToString());
                        item.SubItems.Add(result.Currency);
                        item.SubItems.Add(result.URL);
                        item.SubItems.Add(result.SourceName);
                        item.Tag = result;
                        lvItems.Items.Add(item);
                    }
                }
                else
                {
                    Console.WriteLine("No items have been saved for user: " + m_user.UserName);
                }
            }
        }

        private void btnResultsUnselectAll_Click(object sender, EventArgs e)
        {
            CheckItems(lvSearchResults, false);
        }

        private void btnResultsSelectAll_Click(object sender, EventArgs e)
        {
            CheckItems(lvSearchResults, true);
        }

        private void btnItemsSelectAll_Click(object sender, EventArgs e)
        {
            CheckItems(lvItems, true);
        }

        private void btnItemsDeselectAll_Click(object sender, EventArgs e)
        {
            CheckItems(lvItems, false);
        }

        private void CheckItems(ListView lv, bool check)
        {
            foreach (ListViewItem item in lv.Items)
            {
                item.Checked = check;
            }
        }

        private void basicSearchToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }
    }
}